﻿/*
 * 
 */
namespace Mitc.Portal.Services.Logs
{
    #region ----------refer-------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using Newtonsoft.Json;
    using Mitc.Portal.Core.Entitys.Logs;
    using System.IO;

    #endregion
    public class LogsServices : ILogs
    {
        public void WriteLogs(SystemLog logs, string vfilepath)
        {
            var json = JsonConvert.SerializeObject(logs);
            XmlDocument doc = new XmlDocument();
            XmlDocument node = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "LogGroup");

            if (File.Exists(vfilepath))
            {
                // if the file was exist
                doc.Load(vfilepath);

                XmlElement element = doc.DocumentElement;
                element.AppendChild(doc.ImportNode(node.DocumentElement, true));

                XmlTextWriter xmlWriter = new XmlTextWriter(vfilepath, null);
                xmlWriter.Formatting = System.Xml.Formatting.Indented;
                doc.Save(xmlWriter);
            }
            else
            {
                XmlDocument xmlDoc = new XmlDocument();


                // Write down the XML declaration
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

                // Create the root element
                XmlElement rootNode = xmlDoc.CreateElement("Roots");
                xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
                xmlDoc.AppendChild(rootNode);
                XmlDocument docs = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json, "LogGroup");
                XmlElement element = xmlDoc.DocumentElement;
                element.AppendChild(xmlDoc.ImportNode(docs.DocumentElement, true));

                // Save to the XML file
                xmlDoc.Save(vfilepath);

            }



            //XmlDocument doc=(XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json,"LogGroup");



        }

        public void WriteLogs(ActionLog logs, string vfilepath)
        {
            var json = JsonConvert.SerializeObject(logs);
            XmlDocument doc = new XmlDocument();
            XmlDocument node = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "LogGroup");

            if (File.Exists(vfilepath))
            {
                // if the file was exist
                doc.Load(vfilepath);

                XmlElement element = doc.DocumentElement;
                element.AppendChild(doc.ImportNode(node.DocumentElement, true));

                XmlTextWriter xmlWriter = new XmlTextWriter(vfilepath, null);
                xmlWriter.Formatting = System.Xml.Formatting.Indented;
                doc.Save(xmlWriter);
            }
            else
            {
                XmlDocument xmlDoc = new XmlDocument();


                // Write down the XML declaration
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

                // Create the root element
                XmlElement rootNode = xmlDoc.CreateElement("Roots");
                xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
                xmlDoc.AppendChild(rootNode);
                XmlDocument docs = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json, "LogGroup");
                XmlElement element = xmlDoc.DocumentElement;
                element.AppendChild(xmlDoc.ImportNode(docs.DocumentElement, true));

                // Save to the XML file
                xmlDoc.Save(vfilepath);
            }
        }
        public void TraceLogs(SystemLog logs, string vfilepath)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public void TraceLogs(ActionLog logs, string vfilepath)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public void ViewLogs(string vfilepath, ActionLog logs)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public void ViewLogs(string vfilepath, SystemLog logs)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }
    }
}
